package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Tag;
import com.sfzy.service.TagService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("tag")
public class TagController {
    @Autowired
    private TagService tagService;
    //增加
    @PostMapping("add")
    public R insertTag(@RequestBody Tag tag) {
        int num = tagService.insertTag(tag);
        if (num > 0) {
            return R.ok();
        }else{
            return R.fail();
        }
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteTag(@PathVariable("id") int id) {
        int num = tagService.deleteTag(id);
        if (num > 0) {
            return R.ok();
        }else{
            return R.fail();
        }
    }
    //修改
    @PutMapping("update")
    public R updateTag(@RequestBody Tag tag) {
        int num = tagService.updateTag(tag);
        if (num > 0) {
            return R.ok();
        }else{
            return R.fail();
        }
    }
    //根据id查询
    @GetMapping("{id}")
    public R getTagById(@PathVariable("id") int id) {
        Tag tag = tagService.selectTagById(id);
        return R.ok(tag);
    }
    //分页
    @GetMapping("page")
    public R getTags(Tag tag,
                    @RequestParam(required = false,defaultValue = "1")int pageNum,
                    @RequestParam(required = false,defaultValue = "3")int pageSize){
        PageInfo<Tag> tagPageInfo = tagService.selectAllTags(tag, pageNum, pageSize);
        return R.ok(tagPageInfo);
    }
}
